package org.example.component;

import lombok.extern.slf4j.Slf4j;
import org.example.enums.ProductOrderEnum;
import org.example.vo.PayInfoVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: 谢超
 * @date: 2024/8/21 11:10
 */
@Component
@Slf4j
public class PayFactory {

    @Resource
    private AliPayStrategy aliPayStrategy;

    @Resource
    private WechatPayStrategy wechatPayStrategy;

    private final Map<String, PayStrategy> payStrategyMap = new HashMap<>();

    public PayFactory() {
        // 这里原本采用了context作为上下文去执行策略，但是通过（Spring容器）反射的方式，直接获取到策略类，然后调用方法，更加简洁
        // payStrategyMap.put(ProductOrderEnum.ALI_PAY.name(), new PayStrategyContext(aliPayStrategy));
        // payStrategyMap.put(ProductOrderEnum.WECHAT_PAY.name(), new PayStrategyContext(wechatPayStrategy));
        payStrategyMap.put(ProductOrderEnum.ALI_PAY.name(), aliPayStrategy);
        payStrategyMap.put(ProductOrderEnum.WECHAT_PAY.name(), wechatPayStrategy);
    }

    /**
     * 下单
     * @param payInfoVO
     * @return
     */
    public String pay(PayInfoVO payInfoVO) {
        String payType = payInfoVO.getPayType();
        return payStrategyMap.get(payType).unifiedOrder(payInfoVO);
    }

    /**
     * 关闭订单
     * @param payInfoVO
     * @return
     */
    public String closeOrder(PayInfoVO payInfoVO) {
        String payType = payInfoVO.getPayType();
        return payStrategyMap.get(payType).closeOrder(payInfoVO);
    }

    /**
     * 查询支付状态
     * @param payInfoVO
     * @return
     */
    public String queryPayStauts(PayInfoVO payInfoVO) {
        String payType = payInfoVO.getPayType();
        return payStrategyMap.get(payType).queryPayStatus(payInfoVO);
    }

    /**
     * 退款
     * @param payInfoVO
     * @return
     */
    public String refund(PayInfoVO payInfoVO) {
        String payType = payInfoVO.getPayType();
        return payStrategyMap.get(payType).refund(payInfoVO);
    }
}
